package com.lanyan.common.core.domain;

import com.github.pagehelper.PageInfo;
import com.lanyan.common.constant.HttpStatus;

import java.io.Serializable;
import java.util.List;

/**
 * 响应信息主体
 *
 * @author lanyan
 */
public class PageR<T> implements Serializable
{
    private static final long serialVersionUID = 1L;

    /** 成功 */
    public static final int SUCCESS = HttpStatus.SUCCESS;

    /** 失败 */
    public static final int FAIL = HttpStatus.ERROR;

    private int code;

    private String msg;

    private List<T> rows;

    private long total;

//    public static <T> PageR<T> ok()
//    {
//        return restResult(null, SUCCESS, "操作成功");
//    }
//
//    public static <T> PageR<T> ok(T data)
//    {
//        return restResult(data, SUCCESS, "操作成功");
//    }
//
//    public static <T> PageR<T> ok(T data, String msg)
//    {
//        return restResult(data, SUCCESS, msg);
//    }
//
//    public static <T> PageR<T> fail()
//    {
//        return restResult(null, FAIL, "操作失败");
//    }
//
//    public static <T> PageR<T> fail(String msg)
//    {
//        return restResult(null, FAIL, msg);
//    }
//
//    public static <T> PageR<T> fail(T data)
//    {
//        return restResult(data, FAIL, "操作失败");
//    }
//
//    public static <T> PageR<T> fail(T data, String msg)
//    {
//        return restResult(data, FAIL, msg);
//    }
//
//    public static <T> PageR<T> fail(int code, String msg)
//    {
//        return restResult(null, code, msg);
//    }

    public static <T> PageR<T> page(List<T> list)
    {
        PageR<T> res = new PageR<T>();
        res.setCode(200);
        res.setRows(list);
        res.setTotal(new PageInfo(list).getTotal());
        res.setMsg("查询成功");
        return res;
    }

    public int getCode()
    {
        return code;
    }

    public void setCode(int code)
    {
        this.code = code;
    }

    public String getMsg()
    {
        return msg;
    }

    public void setMsg(String msg)
    {
        this.msg = msg;
    }

    public static <T> Boolean isError(PageR<T> ret)
    {
        return !isSuccess(ret);
    }

    public static <T> Boolean isSuccess(PageR<T> ret)
    {
        return PageR.SUCCESS == ret.getCode();
    }

    public List<T> getRows() {
        return rows;
    }

    public void setRows(List<T> rows) {
        this.rows = rows;
    }

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }
}
